<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>开始使用 layui</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
</head>
<body style="padding-top: 100px" class="layui-bg-black">
<!-- 你的 HTML 代码 start -->

<div class="layui-container ">
    <div class="layui-row">

        <div class="layui-col-md4 layui-col-md-offset4">


            <div class="layui-card layui-bg-gray">
                <div class="layui-card-header">请登录</div>
                <div class="layui-card-body">
                    <form class="layui-form">

                        <div class="layui-form-item">
                            <label class="layui-form-label"><i class="layui-icon layui-icon-username"></i> </label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" required  lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label"><i class="layui-icon layui-icon-password"></i> </label>
                            <div class="layui-input-inline">
                                <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label"><i class="layui-icon layui-icon-vercode"></i> </label>
                            <div class="layui-input-inline">
                                <input type="text" name="captchar" required lay-verify="required" placeholder="请输入验证码" autocomplete="off" class="layui-input">
                            </div>

                        </div>


                        <div class="layui-form-item">
                            <label class="layui-form-label"><i class="layui-icon layui-icon-vercode"></i> </label>
                            <div class="layui-input-inline">
                                <img id="captcharimg" src="/captcha">
                            </div>

                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">免登录</label>
                            <div class="layui-input-block">
                                <input type="checkbox" name="switch" lay-skin="switch">
                            </div>
                        </div>


                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="loginForm">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>


        </div>

    </div>
</div>




<!-- 你的 HTML 代码 end -->

<script src="./layui/layui.js"></script>
<script>
    layui.use( function(){
        var layer = layui.layer
            ,form = layui.form;

        var $ = layui.$;  // 引入jquery的核心对象

        //监听提交
        form.on('submit(loginForm)', function(data){

            console.log(data.field);// 把表单中填好的数据 对应 的js对象 打印出来

            // layer.msg(JSON.stringify(data.field));

            // 发送ajax请求给后端登录接口
            $.ajax({
                url:'/user/login',
                type:'post',
                data: data.field,
                success:function (ret) {
                    if (ret.code == 1){ // 登录成功
                        window.location='/plateform.html';
                    }else {
                        layer.msg(ret.msg);
                    }
                }
            });


            // 取消表单的默认提交行为
            return false;
        });


        // 注册验证码的单击事件
        $("#captcharimg").click(function () {

            $("#captcharimg").attr('src','/captcha?t='+new Date().getTime());

        });

    });



    // $(function () {
    //
    // });
</script>
</body>
</html>
